package com.example.nio.security.custom;


import com.example.nio.security.config.CustomUserDetailsService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.core.context.SecurityContext;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Component;
import org.springframework.util.StopWatch;
import org.springframework.web.filter.OncePerRequestFilter;

import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 *  <p> 访问鉴权 - 每次访问接口都会经过此 </p>
 *
 * @description :
 * @author : zhengqing
 * @date : 2019/10/12 16:17
 */
@Slf4j
@Component
public class MyAuthenticationFilter extends OncePerRequestFilter {

    private final CustomUserDetailsService userDetailsService;

    protected MyAuthenticationFilter(CustomUserDetailsService userDetailsService) {
        this.userDetailsService = userDetailsService;
    }

    @Override
    protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws ServletException, IOException {
        System.out.println("请求头类型： " + request.getContentType());
//        if ((request.getContentType() == null && request.getContentLength() > 0) || (request.getContentType() != null && !request.getContentType().contains(Constants.REQUEST_HEADERS_CONTENT_TYPE))) {
//            filterChain.doFilter(request, response);
//            return;
//        }
        StopWatch stopWatch = new StopWatch();
        try {
//            stopWatch.start();
            //  记录请求的消息体
            SecurityContext context = SecurityContextHolder.getContext();
//            if (context.getAuthentication() != null && context.getAuthentication().isAuthenticated()) {
//                filterChain.doFilter(wrappedRequest, wrappedResponse);
//                return;
//            }
//            String token = "123";
            // 前后端分离情况下，前端登录后将token储存在cookie中，每次访问接口时通过token去拿用户权限
            String token = request.getHeader("Authorization");
            log.debug("后台检查令牌:{}", token);
//            if (StringUtils.isNotBlank(token)) {
//                // 检查token
//                SecurityUser securityUser = userDetailsService.getUserByToken(token);
//                if (securityUser == null || securityUser.getCurrentUserInfo() == null) {
//                    throw new AccessDeniedException("TOKEN已过期，请重新登录！");
//                }
//                UsernamePasswordAuthenticationToken authentication = new UsernamePasswordAuthenticationToken(securityUser, null, securityUser.getAuthorities());
//                // 全局注入角色权限信息和登录用户基本信息
//                SecurityContextHolder.getContext().setAuthentication(authentication);
//            }
            filterChain.doFilter(request, response);
        } finally {
//            stopWatch.stop();
            long usedTimes = stopWatch.getTotalTimeMillis();
            // 记录响应的消息体
        }

    }
}
